package com.system.licence;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;



public class FileManagerTools  
{  
	  
    public static void main(String[] args)  
    {     
        String readfile = "d:" + File.separator + "readfile.properties";  
        String writefile = "d:" + File.separator + "writefile.properties";  
        String readxmlfile = "d:" + File.separator + "readxmlfile.xml";  
        String writexmlfile = "d:" + File.separator + "writexmlfile.xml";  
        String readtxtfile = "d:" + File.separator + "readtxtfile.txt";  
        String writetxtfile = "d:" + File.separator + "writetxtfile.txt";  
  
        readPropertiesFile(readfile); //读取properties文件   
        writePropertiesFile(writefile); //写properties文件   
        readPropertiesFileFromXML(readxmlfile); //读取XML文件   
//        writePropertiesFileToXML(writexmlfile); //写XML文件   
        readPropertiesFile(readtxtfile); //读取txt文件   
        writePropertiesFile(writetxtfile); //写txt文件   
    }  
  
    public static void readFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                System.out.println("line " + line + ": " + tempString);
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }
    //读取资源文件,并处理中文乱码   
    public static void readPropertiesFile(String filename)  
    {  
        Properties properties = new Properties();  
        try  
        {  
            InputStream inputStream = new FileInputStream(filename);  
            properties.load(inputStream);  
            inputStream.close(); //关闭流   
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
        String username = properties.getProperty("username");  
        String passsword = properties.getProperty("password");  
        String chinese = properties.getProperty("chinese");  
        try  
        {  
            chinese = new String(chinese.getBytes("ISO--"), "GBK"); // 处理中文乱码   
        }  
        catch (UnsupportedEncodingException e)  
        {  
            e.printStackTrace();  
        }  
        System.out.println(username);  
        System.out.println(passsword);  
        System.out.println(chinese);  
    }  
    public static String readPropertiesFile(String filename,String key) throws IOException  
    {  
        Properties properties = new Properties();  
        try  
        {  
            InputStream inputStream = new FileInputStream(filename);  
            properties.load(inputStream);  
            inputStream.close(); //关闭流   
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
            throw e;
        }        
        return properties.getProperty(key);
    }  
  
    //读取XML文件,并处理中文乱码   
    public static void readPropertiesFileFromXML(String filename)  
    {  
        Properties properties = new Properties();  
        try  
        {  
            InputStream inputStream = new FileInputStream(filename);  
            properties.loadFromXML(inputStream);  
            inputStream.close();  
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
        String username = properties.getProperty("username");  
        String passsword = properties.getProperty("password");  
        String chinese = properties.getProperty("chinese"); //XML中的中文不用处理乱码，正常显示   
        System.out.println(username);  
        System.out.println(passsword);  
        System.out.println(chinese);  
    }  
  
    //写资源文件，含中文   
    public static void writePropertiesFile(String filename)  
    {  
        Properties properties = new Properties();  
        try  
        {  
            OutputStream outputStream = new FileOutputStream(filename);  
            properties.setProperty("username", "myname");  
            properties.storeToXML(outputStream, "author: shixing_@sina.com");  
            outputStream.close();  
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
    }  
  
}  
